/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
// 合并两个有序链表
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }

        
        ListNode dummy=new ListNode(-1);
        ListNode prev=dummy;
        //prev记录的是最后一个指向的结点

        while(list1!=null&&list2!=null){
            if(list1.val<list2.val){
                prev.next=list1;
                list1=list1.next;
                prev=prev.next;

            }else{
                prev.next=list2;
                list2=list2.next;
                prev=prev.next;
            }
            
            
        }
        //考虑合并完假如就剩一个结点,哪个不为空就插到谁后面
        prev.next=list1==null?list2:list1;
        return dummy.next;

    }
}